package com.vimi8.ebb.auth.api;

import com.vimi8.ebb.auth.configuration.HttpResponseEntity;
import com.vimi8.ebb.auth.configuration.OAuth2AuthenticationImpl;
import com.vimi8.ebb.auth.dto.MessageDTO;
import com.vimi8.ebb.auth.model.Error;
import com.vimi8.ebb.auth.model.MessageQuery;
import com.vimi8.ebb.auth.model.MyMessage;
import com.vimi8.ebb.auth.service.MyMessageService;
import com.vimi8.ebb.auth.utils.MessageUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by think on 2016/12/16.
 */
@RestController
public class MessageController {

    @Autowired
    private MyMessageService myMessageService;

    @ApiOperation(value = "APP-查询我的消息", notes = "根据当前用户id查询我的消息.", response = MessageDTO.class, authorizations = {
//            @Authorization(value = "accessCode", scopes = {
//                    @AuthorizationScope(scope = "ebpa-platform:user", description = "资源ebpa-platform下的读取权限.")
//            })
    }, tags={ "员工相关信息", })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = MessageDTO.class),
            @ApiResponse(code = 200, message = "失败,返回消息", response = MessageDTO.class) })
    @RequestMapping(value = "/my/message",
            method = RequestMethod.GET)
    HttpResponseEntity<List<MessageDTO>> MyMessageGet(
            @ApiParam(value = "消息类型", required = true) @RequestParam(value = "type", required = true) String type
    ){

        List<MessageDTO> messageDTO=null;

        try {
            //获取当前登录用户id
            String openId = OAuth2AuthenticationImpl.openId();

            messageDTO= myMessageService.getMyMessageByType(openId, type);
        } catch (Exception e) {
            return new HttpResponseEntity<List<MessageDTO>>(new Error().error("sql error").errorDescription(e.getLocalizedMessage()), HttpStatus.BAD_GATEWAY);

        }
        return new HttpResponseEntity<List<MessageDTO>>(messageDTO,HttpStatus.OK);
    }


    @ApiOperation(value = "APP-屏蔽消息", notes = "屏蔽对方的消息.", response = String.class, authorizations = {
//            @Authorization(value = "accessCode", scopes = {
//                    @AuthorizationScope(scope = "ebpa-platform:user", description = "资源ebpa-platform下的读取权限.")
//            })
    }, tags={ "员工相关信息", })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 200, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/my/message",
            method = RequestMethod.PUT)
    HttpResponseEntity<String> MyMessagePUT(
            @ApiParam(value = "发信人id", required = true) @RequestParam(value = "releaseId", required = true) String releaseId,
            @ApiParam(value = "消息id", required = true) @RequestParam(value = "messageId", required = true) String messageId
            ){

        int count = 0;
        try {

            count = myMessageService.updateIsScreen(messageId,releaseId);
        }catch (Exception e){
            Error error = new Error().error("sql error").errorDescription(e.getLocalizedMessage());
            return new HttpResponseEntity<String>(error,HttpStatus.OK);
        }
        return new HttpResponseEntity<String>(count+"",HttpStatus.OK);
    }


    @ApiOperation(value = "APP-标记已读信息", notes = "标记已读消息.", response = MessageDTO.class, authorizations = {
//            @Authorization(value = "accessCode", scopes = {
//                    @AuthorizationScope(scope = "ebpa-platform:user", description = "资源ebpa-platform下的读取权限.")
//            })
    }, tags={ "员工相关信息", })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = MessageDTO.class),
            @ApiResponse(code = 200, message = "失败,返回消息", response = MessageDTO.class) })
    @RequestMapping(value = "/my/read/message",
            method = RequestMethod.PUT)
    HttpResponseEntity<String> MyMessageISReadPUT(
            @ApiParam(value = "消息id", required = true) @RequestParam(value = "messageId", required = true) String messageId
    ){
        int count = 0;
        try {

            count = myMessageService.updateIsRead(messageId);
        }catch (Exception e){
            Error error = new Error().error("sql error").errorDescription(e.getLocalizedMessage());
            return new HttpResponseEntity<String>(error,HttpStatus.OK);
        }
        return new HttpResponseEntity<String>(count+"",HttpStatus.OK);
    }


    @ApiOperation(value = "APP-插入消息", notes = "插入消息.", response = MessageDTO.class, authorizations = {

    }, tags={ "员工相关信息", })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = MessageDTO.class),
            @ApiResponse(code = 200, message = "失败,返回消息", response = MessageDTO.class) })
    @RequestMapping(value = "/my/message/add",
            method = RequestMethod.POST)
    HttpResponseEntity<String> addMyMessagePOST(
            @ApiParam(value = "消息参数", required = true) @RequestParam(value = "messageQuery", required = true) MessageQuery messageQuery
    ){
        int message=0;
        //获取到当前登录id 发布人id
        String openId = null;
        try {
            openId = OAuth2AuthenticationImpl.openId();
            MyMessage myMessage = MessageUtil.dealMessageTags(openId,messageQuery, myMessageService);
            message= myMessageService.insertMyMessage(myMessage);
        } catch (Exception e) {
            return new HttpResponseEntity<String>(new Error().error("sql error").errorDescription(e.getLocalizedMessage()),HttpStatus.BAD_GATEWAY);
        }
        return new HttpResponseEntity<String>(message+"",HttpStatus.OK);
    }


}
